package me.send.business.service.mapper;

import java.util.List;
import java.util.Map;

import me.send.business.service.BaseMapper;
import me.send.business.service.bean.UserEvaluateInfoBean;
import me.send.business.service.bean.UserInfoBean;
import me.send.business.service.pojo.User;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

//配置缓存
//@CacheNamespace(size = 512)
public abstract interface UserMapper extends BaseMapper {

	// 使用测试接口 2015年7月1日17:50:15
	@Select(value = "select * from user")
	public abstract List<Map<String, Object>> list_all();

	// 提供给其他mapper文件关联这个用户
	@Select(value = "select userid,nickname,headerpath from user where userid = #{userid}")
	public abstract Map<String, Object> one_user_userid(int userid);

	/** 修改用户信息 2015年6月4日09:07:02 杨中仁 开始 * */

	@Update(value = "update user set headerpath = #{headerpath},updatetime = #{updatetime} where userid = #{userid}")
	public abstract void update_header(User user);

	@Update(value = "update user set updatetime = #{updatetime},username=#{username},sex=#{sex},nickname=#{nickname} where userid = #{userid}")
	public abstract void update_user(User user);

	/** 修改用户信息 2015年6月4日09:07:21 杨中仁 结束 * */

	/**
	 * 根据mac地址检查该手机是否注册过
	 * @param macAddr
	 * @return
	 * @author maxmin
	 * @date 2015年9月18日上午11:32:19
	 */
	@Select (value = "SELECT COUNT(1) FROM `user` WHERE mac_addr = #{macAddr}")
	public abstract int checkMacAddr(String macAddr);
	
	/**
	 * 登录检查mac地址
	 * @param macAddr
	 * @return
	 * @author maxmin
	 * @date 2015年9月18日上午11:39:53
	 */
	@Select (value = "SELECT mac_addr FROM `user` WHERE telephone = #{telephone}")
	public abstract String loginCheckMacAddr(String telephone);
	
	// 用户注册
	@Insert(value = "insert into user (telephone,createtime,password,nickname,openAccountId,isvAccountId,mac_addr) "
			+ "values(#{telephone},#{createtime},#{password},#{nickname},#{openAccountId},#{isvAccountId},#{macAddr}) ")
	public abstract void register(User user);

	// 根据手机号码 修改用户密码
	@Update(value = "update user set password = #{password},updatetime = #{updatetime} where telephone = #{telephone}")
	public abstract void updatePassword(User user);

	// 根据手机号码查询下面的数量
	@Select(value = "select count(1) as total from user where telephone = #{telephone}")
	public abstract int count_telephone(String telephone);

	@Select({ "select userid from user where telephone = #{telephone}" })
	public abstract int userid_user_telephone(String paramString);

	// 用户登录
	@Select(value = "select * from user where telephone = #{telephone} and password = #{password}")
	public abstract User login(User user);

	// 使用刷新方法 2015年5月27日16:22:44
	// @Options(useCache = true, flushCache = false, timeout = 10000) //
	// 请求时间是10秒
	/**
//	@Select(value = "select userid,username,nickname,headerpath,status,sex,money,telephone from user where userid = #{userid}")
//	@Results({
//			@Result(property = "userid", column = "userid", id = true),
//			// 统计已完成的小费 2015年7月20日11:42:55
//			@Result(property = "user_tip", column = "userid", one = @One(select = mapper_orders
//					+ "user_order_tip_status")),
//			// 统计已完成的 订单的数量 2015年7月20日11:43:17
//			@Result(property = "order_count", column = "userid", one = @One(select = mapper_orders
//					+ "user_order_count_status")) })
 * 2015/8/13 tanjianglong修改
 */
	@Select(value = "SELECT userid, username,nickname,headerpath,`status`,sex,	money,telephone,"
			+ "(select IFNULL(SUM(ods.tip),0) as tips from orders ods where userid = #{userid} and status > 7 ) user_tip,"
			+ "(select count(1) as total from orders where userid = #{userid} and status > 7) order_count "
			+ "FROM `user` WHERE	userid = #{userid}")
	public abstract Map<String, Object> refresh(int userid);

	
	
	// 收藏经纪人 2015年7月2日17:16:56
	/**
	 * 根据用户id获取用户名
	 * @param userid
	 * @return
	 * @author maxmin
	 * @date 2015年8月25日下午3:36:01
	 */
	@Select(value="SELECT username,nickname,headerpath,sex,telephone,userid,isvAccountId FROM `user` WHERE userid=#{userid}")
	public abstract User getUserNameById(int userid);
	
	/**
	 * 根据orderid获取用户信息
	 * @param orderid
	 * @return
	 * @author maxmin
	 * @date 2015年9月1日下午3:50:56
	 */
	@Select(value = "SELECT u.userid,u.headerpath,u.nickname,u.sex,u.telephone,"
			+ "(SELECT IF(COUNT(1)>0,SUM(ue.point)/COUNT(ue.point),0)  FROM user_evaluate ue WHERE o.orderid = ue.orderid) stars  "
			+ "FROM `user` u LEFT JOIN orders o ON o.userid = u.userid WHERE o.orderid=#{orderid}")
	public UserInfoBean getUserByOrderId(int orderid);
	
	
	/**
	 * 首页点击用户头像获取用户详情
	 * @param userid
	 * @return
	 * @author maxmin
	 * @date 2015年9月1日下午3:51:04
	 */
	@Select(value = "SELECT DISTINCT ue.content,ue.createtime,p.username,p.headerpath,ue.point,"
			+ "ue.evaluate_id,o.orderid,ue.brokerid,p.sex "
			+ "FROM user_evaluate ue LEFT JOIN orders o ON o.orderid = ue.orderid "
			+ "LEFT JOIN `user` u ON o.userid = u.userid LEFT JOIN popuser p ON ue.brokerid = p.popid "
			+ "WHERE o.userid = (SELECT ods.userid FROM orders ods WHERE ods.orderid=#{orderid})")
	@Results({
		@Result(column="evaluate_id",property="evaluateid")
	})
	public List<UserEvaluateInfoBean> getEvaluates(int orderid); 
	
	@Select(value = "SELECT u.userid,u.headerpath,u.nickname,u.sex,u.telephone,u.isvAccountId "
			+ "FROM `user` u LEFT JOIN orders o ON o.userid = u.userid WHERE o.orderid=#{orderid}")
	public User getUserIsvAccountIdByOrderId(int orderid);
}
